package com.jumi.microservice.service;

import com.jumi.microservice.dto.AliPayTransferResponse;
import com.jumi.microservice.dto.PayTransferQueryRequest;
import com.jumi.microservice.dto.PayTransferRequest;
import com.jumi.microservice.dto.WxPayTransferResponse;
import com.jumi.microservice.transfers.AliPayTransfer;
import com.jumi.microservice.transfers.WxPayTransfer;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Scw
 * @Date 2019-10-08 11:03
 */
@DubboService(cluster = "failfast", timeout = 3000)
public class PayTransferServiceImpl implements IPayTransferService {
    @Resource
    WxPayTransfer wxPayTransfer;
    @Resource
    AliPayTransfer aliPayTransfer;

    /**
     * 企业支付宝打款到个人
     *
     * @param payTransferRequest
     * @return
     */
    @Override
    public AliPayTransferResponse aliPayTransfer(PayTransferRequest payTransferRequest) {
        Map<String, String> returnMap = aliPayTransfer.doProcess(aliPayTransfer.createContext(payTransferRequest));
        //返回信息
        AliPayTransferResponse aliPayTransferResponse = new AliPayTransferResponse();
        //公共返回信息
        aliPayTransferResponse.setCode(returnMap.get("code"));
        aliPayTransferResponse.setMsg(returnMap.get("msg"));
        aliPayTransferResponse.setParameters(returnMap.get("parameters"));
        //成功返回信息
        if (returnMap.containsKey("out_biz_no")) {
            aliPayTransferResponse.setOutBizNo(returnMap.get("out_biz_no"));
        }
        if (returnMap.containsKey("order_id")) {
            aliPayTransferResponse.setOrderId(returnMap.get("order_id"));
        }
        if (returnMap.containsKey("pay_date")) {
            aliPayTransferResponse.setPayDate(returnMap.get("pay_date"));
        }
        //失败返回信息
        if (returnMap.containsKey("sub_code")) {
            aliPayTransferResponse.setSubCode(returnMap.get("sub_code"));
        }
        if (returnMap.containsKey("sub_msg")) {
            aliPayTransferResponse.setSubMsg(returnMap.get("sub_msg"));
        }
        return aliPayTransferResponse;
    }

    /**
     * 企业微信打款到个人
     *
     * @param payTransferRequest
     * @return
     */
    @Override
    public WxPayTransferResponse wxPayTransfer(PayTransferRequest payTransferRequest) {
        Map<String, String> returnMap = wxPayTransfer.doProcess(wxPayTransfer.createContext(payTransferRequest));
        //返回信息
        WxPayTransferResponse wxPayTransferResponse = new WxPayTransferResponse();
        //公共返回信息
        wxPayTransferResponse.setReturnCode(returnMap.get("return_code"));
        wxPayTransferResponse.setReturnMsg(returnMap.get("return_msg"));
        wxPayTransferResponse.setResultCode(returnMap.get("result_code"));
        wxPayTransferResponse.setParameters(returnMap.get("parameters"));
        //成功返回信息
        if (returnMap.containsKey("partner_trade_no")) {
            wxPayTransferResponse.setPartnerTradeNo(returnMap.get("partner_trade_no"));
        }
        if (returnMap.containsKey("payment_no")) {
            wxPayTransferResponse.setPaymentNo(returnMap.get("payment_no"));
        }
        if (returnMap.containsKey("payment_time")) {
            wxPayTransferResponse.setPaymentTime(returnMap.get("payment_time"));
        }
        //失败返回信息
        if (returnMap.containsKey("err_code")) {
            wxPayTransferResponse.setErrCode(returnMap.get("err_code"));
        }
        if (returnMap.containsKey("err_code_des")) {
            wxPayTransferResponse.setErrCodeDes(returnMap.get("err_code_des"));
        }
        return wxPayTransferResponse;
    }

    /**
     * 企业支付宝打款查询
     *
     * @param payTransferQueryRequest
     * @return
     */
    @Override
    public String aliPayTransferQuery(PayTransferQueryRequest payTransferQueryRequest) {
        return aliPayTransfer.transferQuery(payTransferQueryRequest);
    }

    /**
     * 企业微信打款查询
     *
     * @param payTransferQueryRequest
     * @return
     */
    @Override
    public String wxPayTransferQuery(PayTransferQueryRequest payTransferQueryRequest) {
        return wxPayTransfer.transferQuery(payTransferQueryRequest);
    }
}
